/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoEmpregador.java
 *
 * Created on 29/08/2011, 17:31:11
 */
package savet.gui.dialogos;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import savet.entidades.Empregador;

/**
 *
 * @author Adisson Marinho
 */
public class DialogoEmpregador extends javax.swing.JDialog {

    /** Creates new form DialogoEmpregador */
    public DialogoEmpregador(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        rbJuridica.setSelected(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // Ao requisitar fechamento do dialogo, efetuar a operacao de cancelamento.
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                cancelarOperacao();
            }
        });
    }

    private void cancelarOperacao() {
        // anula empregadores
        empregador = null;
        empregadorFisico = null;
        empregadorJuridico = null;
        this.dispose();
    }

    private void confirmarOperacao() {
        if (isCamposPreenchidos()) {
            if (rbJuridica.isSelected()) {
                empregadorJuridico.setNome(txtNome.getText());
                empregadorJuridico.setCNPJ(txtCadastro.getText());
                empregadorJuridico.setRazaoSocial(txtRazao.getText());
            } else {
                empregadorFisico.setNome(txtNome.getText());
                empregadorFisico.setCPF(txtCadastro.getText());
            }
            empregador.setCnae(txtCnae.getText());
            dispose();
        } else {
            lblInfo.setText("Preencha todos os campos!");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup = new javax.swing.ButtonGroup();
        empregadorFisico = new savet.entidades.EmpregadorFisico();
        empregadorJuridico = new savet.entidades.EmpregadorJuridico();
        empregador = new savet.entidades.Empregador();
        painel = new javax.swing.JPanel();
        rbJuridica = new javax.swing.JRadioButton();
        rbFisica = new javax.swing.JRadioButton();
        lblRazao = new javax.swing.JLabel();
        lblCadastro = new javax.swing.JLabel();
        lblNome = new javax.swing.JLabel();
        lblCnae = new javax.swing.JLabel();
        txtRazao = new javax.swing.JTextField();
        txtCadastro = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtCnae = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dados de Empregador");

        painel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        buttonGroup.add(rbJuridica);
        rbJuridica.setText("Pessoa jurídica");
        rbJuridica.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                trocaTipoEmpregadorEvent(evt);
            }
        });

        buttonGroup.add(rbFisica);
        rbFisica.setText("Pessoa física");
        rbFisica.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                trocaTipoEmpregadorEvent(evt);
            }
        });

        lblRazao.setText("Razão social");

        lblCadastro.setText("CPF/CNPJ");

        lblNome.setText("Nome Fantasia");

        lblCnae.setText("CNAE");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, empregadorJuridico, org.jdesktop.beansbinding.ELProperty.create("${razaoSocial}"), txtRazao, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, empregador, org.jdesktop.beansbinding.ELProperty.create("${cadastro}"), txtCadastro, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, empregador, org.jdesktop.beansbinding.ELProperty.create("${nome}"), txtNome, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        btnCancelar.setFont(new java.awt.Font("Tahoma", 0, 10));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnOk.setFont(new java.awt.Font("Tahoma", 0, 10));
        btnOk.setText("Ok");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        lblInfo.setFont(new java.awt.Font("Tahoma", 0, 9));
        lblInfo.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelLayout.createSequentialGroup()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCnae, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblNome, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblCadastro, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblRazao, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRazao, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                            .addComponent(txtCnae, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                            .addComponent(txtCadastro, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelLayout.createSequentialGroup()
                                .addComponent(btnOk)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelar)))
                        .addGap(34, 34, 34)
                        .addComponent(lblInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(rbJuridica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbFisica)))
                .addContainerGap())
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbJuridica)
                    .addComponent(rbFisica))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblRazao)
                    .addComponent(txtRazao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblCadastro)
                    .addComponent(txtCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCnae))
                    .addComponent(txtCnae, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnOk))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void trocaTipoEmpregadorEvent(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_trocaTipoEmpregadorEvent
        if (rbJuridica.isSelected()) {
            lblRazao.setVisible(true);
            txtRazao.setVisible(true);
            lblCadastro.setText("CNPJ");
            lblNome.setText("Nome fantasia");
            empregador = empregadorJuridico;
        } else {
            lblRazao.setVisible(false);
            txtRazao.setVisible(false);
            lblCadastro.setText("CPF");
            lblNome.setText("Nome");
            empregador = empregadorFisico;
        }
    }//GEN-LAST:event_trocaTipoEmpregadorEvent

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        cancelarOperacao();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        confirmarOperacao();
    }

    public Empregador getEmpregador() {
        return empregador;
    }//GEN-LAST:event_btnOkActionPerformed

    private boolean isCamposPreenchidos() {
        boolean retorno = false;
        if (rbJuridica.isSelected()) {
            retorno = !txtRazao.getText().isEmpty();
        }
        retorno = !txtNome.getText().isEmpty() && !txtCadastro.getText().isEmpty();
        return retorno;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DialogoEmpregador dialog = new DialogoEmpregador(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnOk;
    private javax.swing.ButtonGroup buttonGroup;
    private savet.entidades.Empregador empregador;
    private savet.entidades.EmpregadorFisico empregadorFisico;
    private savet.entidades.EmpregadorJuridico empregadorJuridico;
    private javax.swing.JLabel lblCadastro;
    private javax.swing.JLabel lblCnae;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblRazao;
    private javax.swing.JPanel painel;
    private javax.swing.JRadioButton rbFisica;
    private javax.swing.JRadioButton rbJuridica;
    private javax.swing.JTextField txtCadastro;
    private javax.swing.JTextField txtCnae;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtRazao;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
